<?php

namespace App\Http\Controllers\Mgt;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;

use App\Repositories\Eloquent\Mgt\UserRepository as User;

use App;

class UserController extends Controller
{
    protected $user;

    public function __construct(User $user)
    {
        parent::__construct();

        $this->user = $user;
    }



    /**
     * [默认用户列表:后台注册/普通读者]
     * @param Request $request [description]
     * @return [type]           [description]
     */
    public function index(Request $request)
    {
        $query = $request->query('query');

        $users = $this->user->index($query, \App\Models\Mgt\User::USER_TYPE_READER)->paginate(60);

        $users = $this->user->transforms($this->act, $users);

        $paging = round(count($users)/2);

        return view($this->view, [
                        'users'=>$users,
                        'activity'=>$this->act,
                        'paging'=>$paging,
                    ]);
    }



    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view($this->view);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        return $this->user->store($request);
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $user = $this->user->show($id);

        if ($user instanceof JsonResponse && $user->getStatusCode() === 404) {
            return abort('404');
        }

        $user = $this->user->transform($this->act, $user);

        return view($this->view, compact('user'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $user = $this->user->show($id);

        if ($user instanceof JsonResponse && $user->getStatusCode() === 404) {
            return abort('404');
        }

        $user = $this->user->transform($this->act, $user);

        return view($this->view, compact('user'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        return $this->user->renew($request, $id);
    }


    public function view($id)
    {
        $user = $this->user->view($id);
        //dd($user->returns);

        return view($this->view, compact('user'));
    }

    /**
     * [用户信息(打印)]
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public function print($id)
    {
        $user = $this->user->show($id);

        return view($this->view, compact('user'));
    }


}
